﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Threading;
using WPFExcelReport;
namespace WPFExcelReport
{
    public partial class VABPOS
    {
        void DoYGRAV95Work()
        {
            if (IsRun)
            {
                return;
            }
            IsRun = true;
            string path = @"C:\IntexReport\Data\YGRAV95";
            string ITXDBVERSION = "YGR";
            YGRAV95(ITXDBVERSION, path);
        }

        public void YGRAV95(string ITXDBVERSION, string path)
        {
            try
            {
                while (true)
                {
                    DataSet SetJob = GetJob(path);
                    if (SetJob.Tables.Count == 0)
                    {
                        IsRun = false;
                        break;
                    }
                    CreateAB50(ITXDBVERSION, SetJob);
                    string VKPAUFKRS = "", VKPAUFLFD = "", VKPAUFPOS = "", QTYRESERVE = "";
                    DataTable tbMU = SetJob.Tables["ICC_ABAX"];
                    if (tbMU != null)
                    {
                        string filter = "ABAID>0";
                        DataRow[] rows = tbMU.Select(filter);
                        List<DataRow> list = new List<DataRow>();
                        if (rows.Length > 0)
                        {
                            foreach (DataRow row in rows)
                            {
                                DataRow dwABAX = INTEXDBC.Convert2ICC_ABAX(row, app.company, true);
                                dwABAX["ABAID"] = 0;
                                list.Add(dwABAX);
                                if (dwABAX["ABAVKAUFTYP"].ToString().Trim() == "K")
                                {
                                    VKPAUFKRS = dwABAX["ABAVKAUFKRS"].ToString();
                                    VKPAUFLFD = dwABAX["ABAVKAUFLFD"].ToString();
                                    VKPAUFPOS = dwABAX["ABAVKAUFPOS"].ToString();
                                    QTYRESERVE = (Convert.ToDouble(dwABAX["ABAQTYRESERVE"].ToString()) / 10).ToString();
                                    DataRow dwData = QTB.TBMain.NewRow();
                                    dwData["VKPMANDANT"] = dwABAX["ABAMANDANT"].ToString();
                                    dwData["VKPAUFKRS"] = VKPAUFKRS;
                                    dwData["VKPAUFLFD"] = VKPAUFLFD;
                                    dwData["VKPAUFPOS"] = VKPAUFPOS;
                                    dwData["C20"] = QTYRESERVE;
                                    INTEXDBC.UpdateVKPOS(dwData, app.company, 6);
                                    dwData["GAMANDANT"] = dwABAX["ABAMANDANT"].ToString();
                                    dwData["GAAUFKRS"] = VKPAUFKRS;
                                    dwData["GAAUFLFD"] = VKPAUFLFD;
                                    dwData["GAAUFPOS"] = VKPAUFPOS;
                                    dwData["C20"] = QTYRESERVE;
                                    INTEXDBC.UpdateGAAUF(dwData, 6);
                                }
                            }
                        }
                        SaveAB(ITXDBVERSION, list);
                    }
                }
                EventDispatcher.Instance.RaiseStatus("YGRAV95完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                EventDispatcher.Instance.RaiseStatus("YGRAV95异常终止", StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                IsRun = false;
            }
        }
    }
}